<template>
  <div class="login-wrap">
    <div>
      <el-form :model="formData" label-width="auto" style="max-width: 600px">
        <el-form-item label="账号">
          <el-input v-model="formData.userName" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="formData.password" type="password" />
        </el-form-item>
        <div class="btn-wrap">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { request } from '@/utils/request'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user/index'

const router = useRouter()
const userStore = useUserStore()

/** 表单数据 */
const formData = reactive({
  userName: 'bigTable',
  password: 'Woshimima123?S'
})

/** 登录 */
const login = () => {
  if (!formData.userName || !formData.password) {
    ElMessage('请输入账号和密码')
    return
  }

  request({
    url: '/tools/login',
    data: {
      userName: formData.userName,
      pass: formData.password
    }
  }).then((res) => {
    if (res.token) {
      ElMessage.success('登录成功')

      // store存储数据
      userStore.setUserInfo(res)

      // 跳转主页
      router.replace({ path: '/' })
    }
  })
}
</script>

<style lang="less" scoped>
.login-wrap {
  position: relative;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  .btn-wrap {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
